<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>简单工厂</title>
</head>
<body>
<script src="../20180406/Interface.js"></script>
<script>
//    var Bicycleshop=function(){};
//    Bicycleshop.prototype={
//        sellBicycle:function(model){
//            var bicycle;
//            switch(model){
//                case 'the speedster':
//                bicycle=new Speedster();
//                break;
//                case 'the lowster':
//                    bicycle=new Lowster();
//                    break;
//                case 'the comfort':
//                default:
//                    bicycle=new Comport();
//            }
//            Interface.ensureImplements(bicycle,Bicycle);
//            bicycle.assemble();
//            bicycle.wash();
//
//            return bicycle;
//        }
//    }

    var Bicycle=new Interface('Bicycle',['assemble','wash','ride','repair']);
    //Speedster类
    var Speedster=function(){};
    Speedster.prototype={
        assemble:function(){
        },
        wash:function(){

        },
        ride:function(){

        },
        repair:function(){

        }
    }

    //要出售某种型号自行车，只要调用sellBicycle的方法就可以了
    var ca=new Bicycleshop();
    var y=ca.sellBicycle('the speedster');
    //这样的工厂缺点是，你要多生产一个型号那你就要在工厂中多加一个型号，比较麻烦
//解决的办法就是，建一个工厂,我理解的工厂就是，他定义一个创建自行车的方法，让用户自己决定创建哪种方法，
//然后又自行车的一些功能，他放在商店里，你创建的每隔自行车他都可以有自行车商店里的功能。
    var BicycleFactory={
        createBicycle:function(model){
            var bicycle;
            switch (model){
                case 'the speedster':
                    bicycle=new Speedster();
                    break;
                case 'the lowster':
                    bicycle=new Lowster();
                    break;
                case 'the comfort':
                default:
                    bicycle=new Comport();
            }
            Interface.ensureImplements(bicycle,Bicycle);
            return bicycle;
        }
    };

    var Bicycleshop=function(){};
    Bicycleshop.prototype={
        sellBicycle:function(model) {
            var bicycle=BicycleFactory.createBicycle(model);

            bicycle.assemble();
            bicycle.wash();

            return bicycle;
        }
    }

</script>
</body>
</html>